package cn.gcyl.module.biz.service.marketinformation;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.marketinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.marketinformation.MarketInformationDO;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.module.biz.dal.mysql.marketinformation.MarketInformationMapper;
import cn.gcyl.module.biz.dal.mysql.useractionlog.UserActionLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.MARKET_INFORMATION_NOT_EXISTS;

/**
 * 市场资讯 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MarketInformationServiceImpl implements MarketInformationService {

    @Resource
    private MarketInformationMapper marketInformationMapper;
    @Resource
    private UserActionLogMapper userActionLogMapper;


    @Override
    public Integer createMarketInformation(MarketInformationSaveReqVO createReqVO) {
        // 插入
        MarketInformationDO marketInformation = BeanUtils.toBean(createReqVO, MarketInformationDO.class);
        marketInformationMapper.insert(marketInformation);

        // 返回
        return marketInformation.getId();
    }

    @Override
    public void updateMarketInformation(MarketInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateMarketInformationExists(updateReqVO.getId());
        // 更新
        MarketInformationDO updateObj = BeanUtils.toBean(updateReqVO, MarketInformationDO.class);
        marketInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteMarketInformation(Integer id) {
        // 校验存在
        validateMarketInformationExists(id);
        // 删除
        marketInformationMapper.deleteById(id);
    }

    @Override
        public void deleteMarketInformationListByIds(List<Integer> ids) {
        // 删除
        marketInformationMapper.deleteByIds(ids);
        }


    private void validateMarketInformationExists(Integer id) {
        if (marketInformationMapper.selectById(id) == null) {
            throw exception(MARKET_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public MarketInformationDO getMarketInformation(Integer id) {
        return marketInformationMapper.selectById(id);
    }

    @Override
    public PageResult<MarketInformationDO> getMarketInformationPage(MarketInformationPageReqVO pageReqVO) {
        return marketInformationMapper.selectPage(pageReqVO);
    }
    @Override
    public MarketInformationDO getAppMarketInformation(Integer id) {
        MarketInformationDO marketInformationDO = marketInformationMapper.selectById(id);
        Long loginUserId = getLoginUserId();
        if (loginUserId != null) {
            //添加操作日志
            UserActionLogDO userActionLogDO = new UserActionLogDO();
            userActionLogDO.setUserId(loginUserId);
            userActionLogDO.setAction("访问市场咨讯:" + marketInformationDO.getInformationName());
            userActionLogMapper.insert(userActionLogDO);
        }
        return marketInformationDO;
    }

    @Override
    public PageResult<MarketInformationDO> getAppMarketInformationPage(MarketInformationPageReqVO pageReqVO) {
        return marketInformationMapper.getAppMarketInformationPage(pageReqVO);
    }

}